package com.csesteel.crm.dao.impl;

import com.csesteel.common.dao.impl.BaseDaoHibernate4;
import com.csesteel.crm.dao.CrmRoleDao;
import com.csesteel.crm.domain.CrmRole;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2015/5/20 0020.
 */
@Resource
public class CrmRoleImpl extends BaseDaoHibernate4<CrmRole>
        implements CrmRoleDao {
        public Map getCrmRoleByUserId(Integer userId) {
                String sql ="select user_id,role_id from crm_r_user_role where user_id = ?" ;
                Integer[] objects=new Integer[1];
                objects[0] = userId;
                Map crmRole = new HashMap();
                try {
                        List<Object[]> listO = findWithSql(sql,objects);
                        if (listO != null && listO.size() == 1 )
                        {
                                for(Object[] objects1:listO)
                                {
                                        crmRole.put("userId",objects1[0]);
                                        crmRole.put("roleId",objects1[1]);
                                }

                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return crmRole;
        }

        public List<CrmRole> findAllCrmRoleByPage(int pageNo, int pageSize, String name) {
                String hql = null;
                if ((name == null) || ("".equals(name))) {
                        hql = "select p from CrmRole p order by p.roleId desc";
                } else {
                        hql = "select p from CrmRole p where p.roleCode like '%" + name + "%'order by roleId desc";
                }

                return findByPage(hql, pageNo, pageSize);
        }

        public long findCountRoleWithParams(String name) {
                if ((name != null) && (!"".equals(name))) {
                        List<?> l = find("select count(*) from CrmRole p where p.roleCode like '%" + name + "%'order by roleId desc");
                        if (l != null && l.size() == 1) {
                                return (Long) l.get(0);
                        }
                } else {
                        return findCount(CrmRole.class);
                }

                // 返回查询得到的实体总数

                return 0;
        }

        public CrmRole getCrmRoleByCode(String roleCode) {
                List<CrmRole> roles = find("select p from CrmRole p where p.roleCode = ?0"
                        , roleCode);
                if (roles!= null && roles.size() >= 1)
                {
                        return roles.get(0);
                }
                return null;
        }

        public void savePriviledgeByRole(Integer priviledgeId, Integer roleId) {
                String sql ="insert into CRM_R_priviledge_ROLE (PRIVILEDGE_ROLE_ID,priviledge_id,ROLE_ID) values (SEQ_CRM_R_PRIVILEDGE_ROLE.Nextval,"+priviledgeId+","+roleId+")";
                saveWithSql(sql);
        }

        public void deletePriviledgeByRole(Integer roleId) {
                String sql ="delete from CRM_R_priviledge_ROLE p where p.role_id ="+roleId;
                deleteBySQL(sql);
        }
}
